<?
include("../com.inc.php");
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$pid=explode("|",$_REQUEST["pid"]);
$add=$del=$list=$fxs=$can=$_can=$isA=array();
$sel="select fid,pids,dtype from pft_product_sale_list where status=0 and aid=".$_SESSION['memberID'];
$GLOBALS['le']->query($sel);
while($row=$GLOBALS['le']->fetch_assoc()){
	if(strtoupper($row['pids'])=="A"){$isA[$row['fid']]=$row['fid'];}
	else{$can[$row['fid']]=$row['pids'];}
	$fxs[]=$row;
}
$sel="select id from uu_products where id not in (".implode(",",$pid).") and apply_did=".$_SESSION['memberID'];
$GLOBALS['le']->query($sel);
while($row=$GLOBALS['le']->fetch_assoc()){
	$list[]=$row['id'];
}
foreach($pid as $_pid){
	$C=$_REQUEST["C".$_pid]?$_REQUEST["C".$_pid]:array();
	$p=$_REQUEST["p".$_pid]?$_REQUEST["p".$_pid]:array();
	$add=$del=array();
	$fp=$s->get_Dynamic_Price_Merge($_pid,date("Y-m-d"),2)/100;
	
	$sel="select title from uu_jq_ticket where pid=".$_pid;
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();
	$ttitle=$GLOBALS['le']->f("title");
	$tt.=$ttitle."、";
	foreach($C as $k=>$v){
		if($v==""){continue;}
		if(filter_var($v, FILTER_VALIDATE_FLOAT)===false || $v-$fp<0){
			$sel="select dtitle from pft_product_sale_list where dtype=".$k." and aid=".$_SESSION['memberID']." limit 1";
			$GLOBALS['le']->query($sel);
			$GLOBALS['le']->fetch_assoc();
			$dtitle=$GLOBALS['le']->f("dtitle");
			if($dtitle){
				exit("<script>alert('分类".$dtitle."配置的".$ttitle."价格不正确')</script>");
			}
			else{
				unset($C[$k]);
			}
		}
	}
	foreach($p as $k=>$v){
		if($v==""){continue;}
		if ($v >= 10000) {
			exit("<script>alert('价格配置必须限制在10000以内')</script>");
		}
		if(filter_var($v, FILTER_VALIDATE_FLOAT)===false || $v-$fp<0){
			$sel="select dname from pft_member where id=".$k." limit 1";
			$GLOBALS['le']->query($sel);
			$GLOBALS['le']->fetch_assoc();
			$name=$GLOBALS['le']->f("dname");
			exit("<script>alert('分销商".$name."配置的".$ttitle."价格不正确')</script>");
		}
	}
	
	foreach($fxs as $v){
		$pri="";
		if(isset($p[$v['fid']])){
			$pri=$p[$v['fid']];
		}
		elseif(isset($C[$v['dtype']])){
			$pri=$C[$v['dtype']];
		}
		
		if($pri==""){
			if(in_array($v['fid'],$isA)){
				unset($isA[$v['fid']]);
				$can[$v['fid']]=implode(",",$list);
				$_can[$v['fid']]=implode(",",$list);
				$del[]=$v['fid'];
			}
			continue;
		}
		
		$pri-=$fp;
		$str.="(".$v['fid'].",".$_pid.",".($pri*100)."),";
		
		if(!in_array($v['fid'],$isA)){
			$_can[$v['fid']]=$can[$v['fid']].($can[$v['fid']]?",":"").$_pid;
			$can[$v['fid']]=$can[$v['fid']].($can[$v['fid']]?",":"").$_pid;
		}
	}
	$list[]=$_pid;
}
foreach($_can as $k=>$v){
	$upd="update pft_product_sale_list set pids='".$v."' where aid=".$_SESSION['memberID']." and fid=".$k;//echo $upd;
	$GLOBALS['le']->query($upd);
}
if($str){
	$ins="insert into uu_priceset (pid,tid,dprice) values ".trim($str,",");//echo $ins;
	$GLOBALS['le']->query($ins);
}
$ins="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",daction='配置".trim($tt,"、")."的分销价格',rectime=now()";
$GLOBALS['le']->query($ins);
echo "<script>alert('配置成功。如需修改,请到\"分销价格\"页。');window.location='prodlists.html';</script>";
?>